package com.example.tse.rbhttpservice.service.member;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberMessageChangeStatusRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberMessageChangeStatusResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberMessageGetLIstRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberMessageGetLIstResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberMessageGetMomentList;
import com.example.tse.rbhttpservice.service.member.model.MemberMessageGetMomentListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberMessageGetMomentListResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MemberMessageServiceImpl implements MemberMessageService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MemberMessageServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取我的系统消息列表，分为两种类型普通消息和优惠券到期提醒消息，优惠券的消息点击固定跳转到我的优惠券列表（未使用状态）
	 * @param request
	 * @param listener
	 * @return MemberMessageGetLIstResponse
	 */		
	 
	    public void getLIst(MemberMessageGetLIstRequest request, final RemoteServiceListenerT<MemberMessageGetLIstResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "member/message/getLIst";


            new HttpService().get(context, url, params, MemberMessageGetLIstResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberMessageGetLIstResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberMessageGetLIstResponse() : (MemberMessageGetLIstResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberMessageGetLIstResponse() : (MemberMessageGetLIstResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 获取我的发现消息［帖子中评论我的、回复我的评论，点赞消息］，点击跳转到帖子详情，每点击都需要更新
	 * @param request
	 * @param listener
	 * @return MemberMessageGetMomentListResponse
	 */		
	 
	    public void getMomentList(MemberMessageGetMomentListRequest request, final RemoteServiceListenerT<MemberMessageGetMomentListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "member/message/getMomentList";


        
        /**
        message
        getMomentList
        GetMomentList
        获取我的发现消息［帖子中评论我的、回复我的评论，点赞消息］，点击跳转到帖子详情，每点击都需要更新
        GET
        MemberMessage
        MemberMessageGetMomentList
        member/message/getMomentList
        array
        */
	
           final Type type = new TypeToken<List<MemberMessageGetMomentList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MemberMessageGetMomentListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MemberMessageGetMomentList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MemberMessageGetMomentListResponse responce = new MemberMessageGetMomentListResponse();
                 responce.setGetMomentList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MemberMessageGetMomentListResponse());
                 } else {
                     List<MemberMessageGetMomentList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberMessageGetMomentListResponse responce = new MemberMessageGetMomentListResponse();
                     responce.setGetMomentList(items);
                     listener.okData(rudderResponse == null ? new MemberMessageGetMomentListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MemberMessageGetMomentListResponse(), fromWhere);
                 } else {
                     List<MemberMessageGetMomentList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberMessageGetMomentListResponse responce = new MemberMessageGetMomentListResponse();
                     responce.setGetMomentList(items);
                     listener.okData(rudderResponse == null ? new MemberMessageGetMomentListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
	/**
	 * 更改消息为已读状态，在发现消息列表，每点击一次消息都需要调用该接口，commentId、likeId必传一个
	 * @param request
	 * @param listener
	 * @return MemberMessageChangeStatusResponse
	 */		
	 
	    public void changeStatus(MemberMessageChangeStatusRequest request, final RemoteServiceListenerT<MemberMessageChangeStatusResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("commentId",request.getCommentId());
            params.put("linkeId",request.getLinkeId());

        final String url = "member/message/changeStatus";


            new HttpService().post(context, url, params, MemberMessageChangeStatusResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberMessageChangeStatusResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberMessageChangeStatusResponse() : (MemberMessageChangeStatusResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberMessageChangeStatusResponse() : (MemberMessageChangeStatusResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
}